package database;

import entity.Teacher;
import manager.TeacherManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Noi
 */
public class TeacherManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public TeacherManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }

    public void loadData(TeacherManager teacherManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetTeacher}");
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int teacherID = rs.getInt("teacherID");
                String teacherName = rs.getString("TeacherName");
                Date birthDay = rs.getDate("BirthDay");
                boolean gender = rs.getBoolean("Gender");
                float salaryScore = rs.getFloat("SalaryScore");
                float seniorityScore = rs.getFloat("SeniorityScore");
                float allowance = rs.getFloat("Allowance");
                Date dateIn = rs.getDate("DateIn");
                String provinceID = rs.getString("ProvinceID");
                String phone = rs.getString("Phone");
                String address = rs.getString("Address");
                boolean status = rs.getBoolean("Status");
                Teacher teacher = new Teacher(teacherID, provinceID, teacherName, birthDay, gender, salaryScore, seniorityScore, allowance, dateIn, phone, address, status);
                teacherManager.add(teacher);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteTeacher(Teacher obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteTeacher(?)}");
            cstmt.setInt(1, obj.getTeacherID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insertTeacher(Teacher obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertTeacher(?,?,?,?,?,?,?,?,?,?,?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            cstmt.setString(2, obj.getTeacherName());
            cstmt.setDate(3, new java.sql.Date(obj.getBirthday().getTime()));
            cstmt.setBoolean(4, obj.isGender());
            cstmt.setFloat(5, obj.getSalaryScore());
            cstmt.setFloat(6, obj.getSeniorityScore());
            cstmt.setFloat(7, obj.getAllowance());
            cstmt.setDate(8, new java.sql.Date(obj.getDateIn().getTime()));
            cstmt.setString(9, obj.getProvinceID());
            cstmt.setString(10, obj.getPhone());
            cstmt.setString(11, obj.getAddress());
            cstmt.executeUpdate();
            obj.setTeacherID(cstmt.getInt("TeacherID"));
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateTeacher(Teacher obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateTeacher(?,?,?,?,?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1, obj.getTeacherID());
            cstmt.setString(2, obj.getTeacherName());
            cstmt.setDate(3, new java.sql.Date(obj.getBirthday().getTime()));
            cstmt.setBoolean(4, obj.isGender());
            cstmt.setFloat(5, obj.getSalaryScore());
            cstmt.setFloat(6, obj.getSeniorityScore());
            cstmt.setFloat(7, obj.getAllowance());
            cstmt.setDate(8, new java.sql.Date(obj.getDateIn().getTime()));
            cstmt.setString(9, obj.getProvinceID());
            cstmt.setString(10, obj.getPhone());
            cstmt.setString(11, obj.getAddress());
            cstmt.setBoolean(12, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
